package com.huifaapp.mall.activity.Homefragment.ShengqianActvity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.huifaapp.mall.R;

import java.util.ArrayList;
import java.util.List;

public class MingXiActivity extends AppCompatActivity implements View.OnClickListener {
    private List<Fragment> list=new ArrayList<>();
    private ViewPager vp;
    private TextView[] tbs;
    private TextView[] tbss;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ming_xi);
        vp = (ViewPager) findViewById(R.id.pppage);
        imageView = (ImageView) findViewById(R.id.Gvmine_back);
        imageView.setOnClickListener(this);
        TextView tvsheng= (TextView) findViewById(R.id.tvshneg);
        tvsheng.setOnClickListener(this);
        TextView tvhua= (TextView) findViewById(R.id.tvhua);
        tvhua.setOnClickListener(this);
        TextView tvss= (TextView) findViewById(R.id.tvs);
        tvss.setOnClickListener(this);
        TextView tvhh= (TextView) findViewById(R.id.tvh);
        tvhh.setOnClickListener(this);
        tbs = new TextView[]{tvsheng,tvhua};
        tbss = new TextView[]{tvss,tvhh};
        list.add(new Fragmentsheng());
        list.add(new Fragmenthua());
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


                shard2(position);
                shard(position);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }


        });

        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });

        vp.setOffscreenPageLimit(3);

        shard(0);
        shard2(0);
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tvshneg:
                shard2(0);
                shard(0);
                vp.setCurrentItem(0);

                break;
            case R.id.tvhua:
                shard2(1);
                shard(1);
                vp.setCurrentItem(1);

                break;
            case  R.id.Gvmine_back:
                finish();
                break;
        }

    }

    public void shard(int porsion){
        for (int i=0;i<tbs.length;i++){
            if (i==porsion){
                tbs[i].setTextColor(Color.RED);

            }else{
                tbs[i].setTextColor(Color.BLACK);
            }

        }
    }
    public void shard2(int porsion){
        for (int i=0;i<tbss.length;i++){
            if (i==porsion){
                tbss[i].setBackgroundColor(Color.RED);

            }else{
                tbss[i].setBackgroundColor(Color.WHITE);
            }

        }
    }
}
